package com.mxpio.erp.quality.service.impl;

import com.mxpio.erp.common.quality.entity.VEInspectionBillPassRate;
import com.mxpio.erp.common.quality.entity.VInspectionBillRecord;
import com.mxpio.erp.common.quality.service.VEInspectionBillPassRateService;
import com.mxpio.erp.common.quality.service.VInspectionBillRecordService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.lin.Linq;
import com.mxpioframework.jpa.query.Criteria;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class VEInspectionBillPassRateServiceImpl implements VEInspectionBillPassRateService {

    @Override
    @Transactional(readOnly = true)
    public List<VEInspectionBillPassRate> list(Criteria c) {
        return JpaUtil.linq(VEInspectionBillPassRate.class).where(c).list();
    }

    @Override
    @Transactional(readOnly = true)
    public Page<VEInspectionBillPassRate> listPage(Pageable pageAble, Criteria c) {
        return JpaUtil.linq(VEInspectionBillPassRate.class).where(c).paging(pageAble);
    }

    @Override
    @Transactional(readOnly = true)
    public List<VEInspectionBillPassRate> getPassRate(Criteria criteria) {
        Linq linq =JpaUtil.linq(VEInspectionBillPassRate.class);
        return linq.aliasToBean(VEInspectionBillPassRate.class).select("yearMonth","type",
                        linq.criteriaBuilder().sum(linq.root().get("quantity")).alias("quantity"),
                        linq.criteriaBuilder().sum(linq.root().get("receiveQuantity")).alias("receiveQuantity"))
                .where(criteria)
                .groupBy("yearMonth","type")
                .list();
    }
}
